local M = _G.ClientOHOH.class.new(script,_G.ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name)
local ClientOHOH = _G.ClientOHOH
local UIManager = ClientOHOH.load.UIManager();
local TaskGui = ClientOHOH.load.UIManager():CreateUI("TaskGui",require(script.TaskGui))
local CallBackManager = ClientOHOH.load.CallBackManager();
local Task_Cfg = _G.CommonOHOH.config.Task
local EventList = ClientOHOH.load.EventList();
local FunctionList = ClientOHOH.load.FunctionList();
local LanguageHelper= _G.LanguageHelper
function M.new(self,...)
    return M(self,...)
end

function M.ctor(self,parent)
    M.super.ctor(self,parent)
    self.DataStore = self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
    self.player = parent
    self.taskList = {}
    self.taskUpdateConnect = {}
    self.taskCompleteConnect = {}
    self.recieveBtnConnect = {}
end
--初始化任务，监听
function M.InitTask(self)
    local taskList = self.DataStore:GetData("task")
    local RegulartaskList = self.DataStore:GetData("regularTask")
    for taskName, value in pairs(RegulartaskList) do
        -- print("index:",taskName,"     value:",value)
        local missionIndex = TaskGui:Init(taskName,taskList[taskName])
        local taskId = value.taskId
        local className = Task_Cfg[taskId].className
        self:CreateTaskInfoConnect(taskName,taskId,className,missionIndex)
        if not value.isClaim then
            self.recieveBtnConnect[taskName]=CallBackManager:AddRbxConnect(TaskGui,TaskGui.missionList[missionIndex].recieveBtn.Activated,function()
                print("-------------领取成功")
                --返回true表示有下一个任务，返回false表示当前任务为最后的任务
                local isNext = self:Invoke(FunctionList.claim_award,taskName)
                if isNext then
                    local nextId = Task_Cfg[taskId].nextId
                    local nextClassName = Task_Cfg[nextId].className
                    TaskGui:UpdateNextTask(missionIndex,nextId)
                    TaskGui:IsShowClaim(missionIndex,false)
                    self:CreateTaskInfoConnect(taskName,nextId,nextClassName,missionIndex)
                else
                    self.recieveBtnConnect[taskName]:Disconnect()
                    TaskGui.missionList[missionIndex].recieveBtn:WaitForChild("TextLabel").Text = LanguageHelper:GetText(10404)
                end
            end)
        else
            TaskGui.missionList[missionIndex].recieveBtn:WaitForChild("TextLabel").Text = LanguageHelper:GetText(10404)
        end
    end
    
end


--创建任务更新事件
function M.CreateTaskInfoConnect(self,taskName,taskId,className,missionIndex)
    local taskClass = _G.CommonOHOH.load[className]()
    self.taskList[taskName] =  taskClass.new(self.parent,Task_Cfg[taskId].goalValue,taskName);
    self.taskUpdateConnect[taskName]=self.taskList[taskName].UpdateEvent:Connect(function()
        local now,finish= self.taskList[taskName]:GetProgress();
        TaskGui:UpdateProgress(now,finish,missionIndex)
    end)
    self.taskCompleteConnect[taskName] = self.taskList[taskName].CompleteEvent:Connect(function()
        if self.taskCompleteConnect[taskName] then
            self.taskCompleteConnect[taskName]:Disconnect();
            self.taskCompleteConnect[taskName]=nil;
        end
        TaskGui:IsShowClaim(missionIndex,true)
        UIManager.ui_list.HomeGui:SetQuestRed(1)
        print("任务完成：",taskName)
    end)
end

function M.dtor(self)
    M.super.dtor(self)
end

M:RegistEvent(EventList.init_task,M.InitTask)

return M 